package com.woniuxy.portal.config.RedisTemplate;

import com.woniuxy.portal.model.vo.CartVo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 配置类里加一个专门的 RedisTemplate<String, CartVo> Bean
 */
@Configuration
public class RedisTemplateBean {

    @Bean
    public RedisTemplate<String, CartVo> cartVoRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, CartVo> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 序列化配置（重要，不然存进去是乱码）
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return template;
    }

}
